$(function(){
    var equipment=/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)?"Android":"PC";
    if(equipment=="Android") {
        var none = false;
        $("#off").addClass("no_none");
        /**
         * 手机端退出键
         */
        alert("很抱歉，由于手机端无法支持webSocket，因此聊天功能手机端可能无法使用！");
        $("#Message").append("<div class='text-center'>\n"+
            "<span id='home'></span>\n" +
            "</div>");
        /**
         * 手机端切换功能
         */
        $("#userMessage").click(function () {
            if (none) {
                $("#MyMessage").removeClass("none");
                $("#Images").removeClass("no_none");
                $("#userMessage").find("span").remove();
                $("#userMessage").append("<span class='glyphicon glyphicon-user'></span>");
                none = false;
            } else {
                $("#MyMessage").addClass("none");
                $("#Images").addClass("no_none");
                $("#userMessage").find("span").remove();
                $("#userMessage").append("<span class='glyphicon glyphicon-globe'></span>");
                none = true;
            }
        });

        /**
         * 手机端退出
         */
        $("#home").click(function(){
            if(confirm("是否确认退出？")){
                $.session.remove("User");
                $(location).prop('href', '/')
            }
        });
    }else if(equipment=="PC"){
        /**
         * PC端模态框出现位置
         */
        $(".modal").css({
            'margin-top': ($(window).height() / 12)
        });
    }

    /**
     * PC端退出
     */
    $("#off").click(function(){
        if(confirm("是否确认退出？")){
            $.session.remove("User");
            $(location).prop('href', '/')
        }
    });

    /**
     *图片懒加载
     */
    /*------------------------------------------------------------------*/
    $.post("/Request/Images/findAll",null,function(result){
        var Index=9;
        var images=JSON.parse(result);
        if(images.length>0) {
            $.each(images, function (index, value) {
                if (index < 9) {
                    $("#Image").append("<div class='img-thumbnail text-center'>\n" +
                        "                        <div><img src='" + value.imageLocalPath + "'/></div>\n" +
                        "                        <div class='text-center' style='font-family: newFontFamily;font-size: 18px'>" + value.imageName + "</div>\n" +
                        "</div>");
                }
            });
            if (Index < images.length) {
                $(window).scroll(function () {
                    if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                        for (var i = 0; i < 3; i++) {
                            if (Index >= 9) {
                                $("#Image").append("<div class='img-thumbnail text-center'>\n" +
                                    "                        <div><img src='" + images[Index].imageLocalPath + "'/></div>\n" +
                                    "                        <div class='text-center' style='font-family: newFontFamily;font-size: 18px'>" + images[Index].imageName + "</div>\n" +
                                    "</div>");
                                Index++;
                            }
                        }
                    }
                });
            }
        }else{
            $("#Image").append("<h3 class='text-center' style='font-family: newFontFamily'>很抱歉,暂无图片资源,请谅解!</h3>");
        }
    });
    /*------------------------------------------------------------------*/

    /*
    * 收件箱
    * */
    /*----------------------------------------------------------------------*/
    var reqPerson=$("#meNumber").text();
    $.post("/findByFriend",{"ReqPerson":reqPerson},function(result){
        var resultMessage=result;
        var outTime;
        if(resultMessage!="暂无消息"){
            var i=0;
            resultMessage=JSON.parse(result);
            outTime=setInterval(function(){
                if(i==50){
                    i=0;
                }
                if(i%2==0) {
                    $("#email").css({"color": "#ff99cc"});
                }else{
                    $("#email").css({"color": "black"});
                }
                i++;
            }, 500);
        }
        $(document).on("click", '#email', function () {
            $("#myModalLabel").text("收件箱");
            $(".modal-body").html("").append("<div id='Inbox'></div>");
            $(".modal-footer").html("");
            if(resultMessage!="暂无消息") {
                $.each(resultMessage, function (index, value) {
                    $("#Inbox").append("<div class='mess'>用户<span style='color:orangered' alt='" + value.resPerson + "'>" + value.reqPerson + "</span>请求加好友<span style='float: right'><a class='yes'>同意</a><a class='no'>拒绝</a></span></div>");
                });
            }else{
                $("#Inbox").append("<div class='mess text-center'>"+resultMessage+"</div>");
            }
            clearInterval(outTime);
            $("#email").css({"color": "black"});
        });
    });

    /**
     * 同意
     */
    $(document).on("click", '.yes', function () {
        var reqPerson=$(this).parent().prev().text();
        var resPerson=$(this).parent().prev().attr("alt");
        $.ajaxRequest(reqPerson,resPerson,"yes",$(this));
    });
    /**
     * 拒绝
     */
    $(document).on("click", '.no', function () {
        var reqPerson=$(this).parent().prev().text();
        var resPerson=$(this).parent().prev().attr("alt");
        $.ajaxRequest(reqPerson,resPerson,"no",$(this));
    });

    /**
     * 添加好友请求jquery插件
     */
    $.extend({
        "ajaxRequest":function(reqPerson,resPerson,Boolean,element) {
            $.post("/IsFriend", {"reqPerson": reqPerson, "resPerson": resPerson, "Boolean": Boolean}, function (result) {
                alert(result);
                element.parent().html("").text(result);
            });
        }
    });
    /*----------------------------------------------------------------------*/

    /**
     * 模态框背景加载
     */
    var ImageNum=0;
    setInterval(function(){
        $(".modal-content").css({
            "background-image":"url(/image/"+ImageNum+".jpg)"
        });
        if(ImageNum==15){
            ImageNum=0;
        }else{
            ImageNum++;
        }
    }, 10000);

    /**
     * js使滑动条处于最下方
     */
    var scroll;
    $(document).on("mouseout", '#getMessage', function () {
        scroll=setInterval(function(){
            $("#getMessage").scrollTop($('#getMessage').prop("scrollHeight"));
        },100);
    });
    $(document).on("mouseover", '#getMessage', function () {
        clearInterval(scroll);
    });
    /**
     * 点击和谁聊天，获取该聊天的数据
     */
    $(document).on("click",".friend",function(){
        var sender =$(this).attr("alt");
        var receiver=$("#meNumber").text();
        $.post("/getMessages",{"sender":sender,"receiver":receiver},function(result){
            var resultMessage=JSON.parse(result);
            $.each(resultMessage,function(index,value){
                $("#getMessage").append(value);
            });
            $(".dataMessage").children("div:last-child").addClass("cssStyle");
        });
    });

});